package com.ky.tms.controller;


import com.ky.tms.entity.TmsCost;
import com.ky.tms.entity.TmsInvoice;
import com.ky.tms.entity.TmsOrder;
import com.ky.tms.exception.PredictableException;
import com.ky.tms.service.ITmsInvoiceService;
import com.ky.tms.util.CommonResult;
import com.ky.tms.util.JwtUtil;
import com.ky.tms.vo.InvoiceOrders;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chengkaimao
 * @since 2020-04-22
 */
@RestController
@RequestMapping("/tms/tms-invoice")
public class TmsInvoiceController {

    @Autowired
    private ITmsInvoiceService tmsInvoiceService;


    @ApiOperation("校验是否能开票")
    @PostMapping("check")
    public CommonResult checkInvoice(@RequestBody List<TmsOrder> orders) throws PredictableException {
        return CommonResult.buildSuccess(tmsInvoiceService.checkInvoice(orders));
    }

    @ApiOperation("开票")
    @PostMapping
    public CommonResult invoice(@RequestBody InvoiceOrders invoiceOrders, HttpServletRequest request) throws PredictableException {
        String userName = JwtUtil.getUserName(request);
        tmsInvoiceService.invoice(userName, invoiceOrders);
        return CommonResult.buildSuccess(null);
    }

    @ApiOperation("收款分页数据")
    @GetMapping("gathering-page")
    public CommonResult gatheringPage(TmsInvoice tmsInvoice, @RequestParam Long pageSize, @RequestParam Long current, HttpServletRequest request) {
        String userName = JwtUtil.getUserName(request);
        return CommonResult.buildSuccess(tmsInvoiceService.gatheringPage(userName,tmsInvoice,pageSize, current));
    }

    @ApiOperation("收款")
    @PostMapping("gathering")
    public CommonResult gathering(@RequestBody TmsInvoice tmsInvoice, HttpServletRequest request) throws PredictableException {
        String userName = JwtUtil.getUserName(request);
        tmsInvoiceService.gathering(userName, tmsInvoice);
        return CommonResult.buildSuccess(null);
    }

    @ApiOperation("确认收款")
    @PostMapping("confirm")
    public CommonResult confirm(@RequestBody TmsInvoice tmsInvoice, HttpServletRequest request) throws PredictableException {
        String userName = JwtUtil.getUserName(request);
        tmsInvoiceService.confirm(userName, tmsInvoice);
        return CommonResult.buildSuccess(null);
    }

}
